﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class ExamDetail
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }

        public int CourseId { get; set; }
        public string CourseName { get; set; }

        public int CourseSubjectId { get; set; }
        public string CourseSubjectName { get; set; }

        public int ExamId { get; set; }
        public string ExamName { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class ExamDetailList : List<ExamDetail>
    {
        public MyError Error { get; set; }
        public ExamDetailList()
        {
        }
    }

    public class ExamDetailDB
    {
        MyDatabase _db = new MyDatabase();

        public ExamDetail Save(ExamDetail ExamDetail)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", ExamDetail.Id);
                _param.Add("@CourseSubjectID", ExamDetail.CourseSubjectId);
                _param.Add("@ExamID", ExamDetail.ExamId);
                if (ExamDetail.Description != string.Empty)
                {
                    _param.Add("@Description", ExamDetail.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", ExamDetail.Name);
                _param.Add("@IsActive", ExamDetail.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_ExamDetailInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    ExamDetail.Error.ErrorNo = 0;
                }
                else
                {
                    ExamDetail.Error.ErrorNo = 1;
                    ExamDetail.Error.ErrorMessage = "No row affect when saving school's ExamDetail.";
                }
            }
            catch (Exception Ex)
            {
                ExamDetail.Error.ErrorNo = 10002;
                ExamDetail.Error.ErrorLocation = "ExamDetail";
                ExamDetail.Error.ErrorMessage = Ex.Message;
                ExamDetail.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "ExamDetail");
            }

            return ExamDetail;
        }

        public ExamDetailList GetExamDetailList(int CompanyId)
        {
            ExamDetailList retValue = new ExamDetailList();

            try
            {
                DataTable _dt = new DataTable("tblExamDetail");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_ExamDetailSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        ExamDetail _ExamDetail = new ExamDetail();
                        FillData(_dr, _ExamDetail);
                        retValue.Add(_ExamDetail);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "ExamDetail";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "ExamDetail");
            }

            return retValue;
        }

        public ExamDetail GetExamDetail(int Id)
        {
            ExamDetail retValue = new ExamDetail();

            try
            {

                DataTable _dt = new DataTable("tblExamDetail");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_ExamDetailSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "ExamDetail";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "ExamDetail");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, ExamDetail ExamDetail)
        {
            ExamDetail.Error = new MyError();
            ExamDetail.Id = Convert.ToInt32(aRow["Id"].ToString());
            ExamDetail.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            ExamDetail.CourseId = Convert.ToInt32(aRow["CourseId"].ToString());
            ExamDetail.CourseSubjectId = Convert.ToInt32(aRow["CourseSubjectId"].ToString());
            ExamDetail.ExamId = Convert.ToInt32(aRow["ExamId"].ToString());
            
            if (aRow["Description"] != DBNull.Value)
            {
                ExamDetail.Description = aRow["Description"].ToString();
            }
            else
            {
                ExamDetail.Description = string.Empty;
            }

            ExamDetail.Name = aRow["Name"].ToString();
            ExamDetail.CompanyName = aRow["CompanyName"].ToString();
            ExamDetail.CourseName = aRow["CourseName"].ToString();
            ExamDetail.CourseSubjectName = aRow["CourseSubjectName"].ToString();
            ExamDetail.CourseSubjectName = aRow["ExamName"].ToString();

            ExamDetail.CreatedDate = Convert.ToDateTime(aRow["CreatedDate"].ToString());
            ExamDetail.ModifiedDate = Convert.ToDateTime(aRow["ModifiedDate"].ToString());

            ExamDetail.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            ExamDetail.Error.ErrorNo = 0;
        }
    }
}
